#include "ShowFindUpdateSDK.h"
#include <sstream>
#include <algorithm>

ShowFindUpdateSDK::ShowFindUpdateSDK()
{
}
bool ShowFindUpdateSDK::compare(Customer c1, Customer c2)
{
	return c1.getCust_name().compare(c2.getCust_name()) <= 0;
}
void ShowFindUpdateSDK::show(vector<Customer> custs)
{
	stringstream cust; //string container
	vector<Customer>::iterator pos; //vector iterator
	sort(custs.begin(),custs.end(),compare); //sorting
	cout << "Show Customers \n";
	for(pos = custs.begin();pos != custs.end();pos++) //get info from vector
	{
		cust << (*pos).display() << "\n";
	}
	cout << cust.str(); //return info
}
void ShowFindUpdateSDK::find(vector<Customer> custs, vector<Cart> carts, vector<Cart_item> cart_items, vector<Product> prods)
{
	int id, total=0; //id for cust_id, and total for total sales
	bool found = false; 
	stringstream res;
	cout << "Find Query Sales for Customer\nID : "; cin >> id; //get cust_id
	vector<Cart>::iterator posCartMain; //vector iterator for Cart in main looping
	vector<Customer>::iterator posCust; //vector iterator for Customer
	for(posCust = custs.begin();posCust != custs.end() && !found;posCust++) //get info customer from vector
	{
		if((*posCust).getCust_id()==id) {
			res << (*posCust).display() << "\n";
			found = true;
		}
	}
	res << "Detail Total Sales\n";
	for(posCartMain = carts.begin();posCartMain != carts.end();posCartMain++) //get info from vector to count total sales
	{
		int cart_id;
		if((*posCartMain).getCust_id()==id) {
			++total; // add total sales
			//process query : cart_id(carts)->prod_id(cart_item)->prod_name(prod)
			vector<Cart>::iterator posCartSub; //iterator vector cart in sub looping
			vector<Product>::iterator posProd; //iterator vector prods
			vector<Cart_item>::iterator posItem; //iterator vector cart_item
			cart_id = (*posCartMain).getCart_id(); 
			int sub =0, prod_id=0; //sub : total for a product
			//find products
			found = false;
			for(posItem = cart_items.begin(); posItem != cart_items.end() && !found ; posItem++)
			{
				if(cart_id == (*posItem).getCart_id())
				{
					found = true;
					prod_id = (*posItem).getProd_id();
				}
			}
			found = false;
			for(posProd = prods.begin(); posProd != prods.end() && !found; posProd++)
			{
				if(prod_id == (*posProd).getProd_id())
				{
					res << (*posProd).getProd_name() << " = ";
					found = true;
				}
			}
			//count a total for each product
			for(posCartSub = carts.begin(); posCartSub != carts.end(); posCartSub++)
			{
				if(cart_id == (*posCartSub).getCart_id() && (*posCartSub).getCust_id()==id)
				{
					++sub;
				}
			}
			res << sub << "\n";
		}
	}
	res << "Total Sales = " << total << "\n";
	cout<< res.str();
}
void ShowFindUpdateSDK::update(vector<Customer> &custs)
{
	cout << "Add Customer " <<endl;
	string cust_name, addr_name;
	int cust_id, addr_num, addr_zip;
	cout << "Cust ID\t\t: "; cin >> cust_id; 
	cout << "Cust Name\t: "; cin >> cust_name;
	cout << "Street Name\t: "; cin >> addr_name;
	cout << "Number\t\t: "; cin >> addr_num;
	cout << "Zip\t\t: "; cin >> addr_zip;
	custs.push_back(Customer(cust_id, cust_name, Address(addr_num, addr_name, addr_zip))); //add customer
}